<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Simple
 *
 * @author ananda
 */
class Simple
{
    protected $_data    = array();
    protected $template = null;
    protected $module   = null;
    protected $space    = '_';

    public function __construct($base_dir, $module_dir, $template_dir) {
        $plugin_class && ($this->plugin_class = $plugin_class);
        $this->setDataSpace();

        parent::__construct($base_dir, $module_dir, $template_dir);
    }

    public function __set($name, $value)
    {
        $this->_data[$this->space][$name] = $value;
    }

    public function __get($name)
    {
        return $this->_data[$this->space][$name];
    }

    public function __isset($name)
    {
        return isset($this->_data[$this->space][$name]);
    }

    public function __unset($name)
    {
        unset($this->_data[$this->space][$name]);
    }

    public function __call($name, $arguments)
    {
        return call_user_func_array(array($this->plugin_class, $name), $arguments);
    }

    public function setTemplate($template, $module = null)
    {
        $this->template = $template;
        $this->module   = $module;
    }

    public function resetData($space = '_')
    {
        $this->_data[$space] = array();
    }

    public function getData($space = '_')
    {
        return $this->_data[$space];
    }

    public function setDataSpace($space = '_')
    {
        $this->space = $space;
    }

    public function getStatus()
    {
        return array('space'    => $this->space,
                     'module'   => $this->module,
                     'template' => $this->template,);
    }

    public function output($space = null)
    {
        !$space && ($space = $this->space);
        isset($this->_data[$space]) && extract($this->_data[$space]);
        require $this->getTemplate();
    }

    public function includeTemplate($template, $module = null, $space = null)
    {
        !$space && ($space = $this->space);
        isset($this->_data[$space]) && extract($this->_data[$space]);
        require $this->getTemplate($template, $module);
    }

    protected function getTemplate($module = null, $template = null)
    {
        !$module   && ($module   = $this->module);
        !$template && ($template = $this->template);
        return $this->module_dir.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.
               $this->template_dir.DIRECTORY_SEPARATOR.$template;
    }
}
